package com.longcai.service.impl.property;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.entity.customer.ClyjUserProperty;
import com.longcai.entity.property.ClyjUserPropertyRecord;
import com.longcai.entity.sys.ClyjDict;
import com.longcai.mapper.dict.ClyjDictMapper;
import com.longcai.mapper.property.ClyjUserPropertyRecordMapper;
import com.longcai.service.property.ClyjUserPropertyRecordService;
import com.longcai.util.DateUtil;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户资产收益记录表 服务实现类
 * </p>
 *
 * @author M
 * @since 2019-04-29
 */
@Service
public class ClyjUserPropertyRecordServiceImpl extends ServiceImpl<ClyjUserPropertyRecordMapper, ClyjUserPropertyRecord> implements ClyjUserPropertyRecordService {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjUserPropertyRecordMapper clyjUserPropertyRecordMapper;

    /**
     * 用户余额明细
     *
     * @param userid
     * @return
     */
    @Override
    public ApiResult getAccount(String userid) {
        // 获得本月第一天0点时间
        String m = DateUtil.getTimesMonthmorning();
        String date = DateUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss");
        try {
            //查询用户资产表
            QueryWrapper<ClyjUserPropertyRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userid);
            queryWrapper.eq("asset_type", "3");
            queryWrapper.between("create_time", m, date);
            List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
            logger.debug("查询成功");
            return ApiResult.success(clyjUserPropertyRecordList);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
            return ApiResult.fail(e.getMessage());
        }
    }

    /**
     * 筛选
     *
     * @param params
     * @param userid
     * @return
     */
    @Override
    public ApiResult selectAccount(JSONObject params, String userid) {
        String value = params.getString("value");

        try {
            QueryWrapper<ClyjUserPropertyRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userid);
            queryWrapper.eq("asset_type", "3");
            //判断传递过来的条件
            //全部
            if (value.equals("0")) {

            }
            //收入
            if (value.equals("1")) {
                queryWrapper.eq("expenses", "0");
            }
            //提现
            if (value.equals("2")) {
                queryWrapper.eq("expenses", "0");
                queryWrapper.eq("flag", "2");
            }
            //其他消费
            if (value.equals("3")) {
                queryWrapper.eq("expenses", "0");
                queryWrapper.eq("flag", "3");
            }
            List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
            logger.debug("查询成功");
            return ApiResult.success(clyjUserPropertyRecordList);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
            return ApiResult.fail(e.getMessage());
        }
    }

    /**
     * 时间筛选
     *
     * @param params
     * @param userid
     * @return
     */
    @Override
    public ApiResult select(JSONObject params, String userid) {
        //类型（1本周 2本月 3 近3月）
        String type = params.getString("type");
        //结束时间
        String endTime = params.getString("endTime");
        //开始时间
        String beginTime = params.getString("beginTime");
        // 获得本周一0点时间
        String w = DateUtil.getTimesWeekmorning();
        // 获得本月第一天0点时间
        String m = DateUtil.getTimesMonthmorning();
        //近三个月的开始时间
        String ms = DateUtil.getCurrentThreeMonthStartTime();
        //近六个月的开始时间
        String mx = DateUtil.getCurrentSixMonthStartTime();
        String date = DateUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss");
        try {
            //查询用户资产表
            QueryWrapper<ClyjUserPropertyRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userid);
            queryWrapper.eq("asset_type", "3");
            if ((type == null || type.equals("")) && (beginTime == null || beginTime.equals("")) && (endTime == null || endTime.equals(""))) {
                queryWrapper.between("create_time", m, date);
                List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
                logger.debug("查询成功");
                return ApiResult.success(clyjUserPropertyRecordList);
            }
            //本周
            if (type.equals("1")) {
                queryWrapper.between("create_time", w, date);
                List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
                logger.debug("查询成功");
                return ApiResult.success(clyjUserPropertyRecordList);
            }
            //本月
            if (type.equals("2")) {
                queryWrapper.between("create_time", m, date);
                List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
                logger.debug("查询成功");
                return ApiResult.success(clyjUserPropertyRecordList);
            }
            //近三月
            if (type.equals("3")) {
                queryWrapper.between("create_time", ms, date);
                List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
                logger.debug("查询成功");
                return ApiResult.success(clyjUserPropertyRecordList);
            }
            //自定义开始和结束时间(只有开始时间)
            if (beginTime != null || !beginTime.equals("") && endTime == null || endTime.equals("")) {
                queryWrapper.between("create_time", beginTime, date);
                List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
                logger.debug("查询成功");
                return ApiResult.success(clyjUserPropertyRecordList);
            }
            //自定义开始和结束时间(只有结束时间查往前6个月)
            if (endTime != null || !endTime.equals("") && beginTime == null || beginTime.equals("")) {
                queryWrapper.between("create_time", mx, endTime);
                List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
                logger.debug("查询成功");
                return ApiResult.success(clyjUserPropertyRecordList);
            }
            //自定义开始和结束时间(查开始和结束)
            if (beginTime != null || !beginTime.equals("") && endTime != null || !endTime.equals("")) {
                queryWrapper.between("create_time", beginTime, endTime);
                List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
                logger.debug("查询成功");
                return ApiResult.success(clyjUserPropertyRecordList);
            }
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
            return ApiResult.fail(e.getMessage());
        }
        return null;
    }


    /**
     * 时间筛选
     *
     * @param params
     * @param userid
     * @return
     */
    @Override
    public ApiResult selectall(JSONObject params, String userid) {
        //类型（1本周 2本月 3 近3月）
        String type = params.getString("type");
        //结束时间
        String endTime = params.getString("endTime");
        //开始时间
        String beginTime = params.getString("beginTime");
        //类型（0 支出 1收入）
        String flag = params.getString("flag");
        //资产类型(0 零钱 1红包 2积分,3余额)
        String asset = params.getString("asset");
        // 获得本周一0点时间
        String w = DateUtil.getTimesWeekmorning();
        // 获得本月第一天0点时间
        String m = DateUtil.getTimesMonthmorning();
        //近三个月的开始时间
        String ms = DateUtil.getCurrentThreeMonthStartTime();
        //近六个月的开始时间
        String mx = DateUtil.getCurrentSixMonthStartTime();
        String date = DateUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss");
        try {
            //查询用户资产表
            QueryWrapper<ClyjUserPropertyRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userid);
            queryWrapper.eq("asset_type", asset);
            if (flag == null || flag.equals("")) {

            } else {
                queryWrapper.eq("expenses", flag);
            }
            if ((type == null || type.equals("")) && (beginTime == null || beginTime.equals("")) && (endTime == null || endTime.equals(""))) {
                queryWrapper.between("create_time", m, date);
                List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
                logger.debug("查询成功");
                return ApiResult.success(clyjUserPropertyRecordList);
            }
            //本周
            if (type.equals("1")) {
                queryWrapper.between("create_time", w, date);
                List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
                logger.debug("查询成功");
                return ApiResult.success(clyjUserPropertyRecordList);
            }
            //本月
            if (type.equals("2")) {
                queryWrapper.between("create_time", m, date);
                List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
                logger.debug("查询成功");
                return ApiResult.success(clyjUserPropertyRecordList);
            }
            //近三月
            if (type.equals("3")) {
                queryWrapper.between("create_time", ms, date);
                List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
                logger.debug("查询成功");
                return ApiResult.success(clyjUserPropertyRecordList);
            }
            //自定义开始和结束时间(只有开始时间)
            if (beginTime != null || !beginTime.equals("") && endTime == null || endTime.equals("")) {
                queryWrapper.between("create_time", beginTime, date);
                List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
                logger.debug("查询成功");
                return ApiResult.success(clyjUserPropertyRecordList);
            }
            //自定义开始和结束时间(只有结束时间查往前6个月)
            if (endTime != null || !endTime.equals("") && beginTime == null || beginTime.equals("")) {
                queryWrapper.between("create_time", mx, endTime);
                List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
                logger.debug("查询成功");
                return ApiResult.success(clyjUserPropertyRecordList);
            }
            //自定义开始和结束时间(查开始和结束)
            if (beginTime != null || !beginTime.equals("") && endTime != null || !endTime.equals("")) {
                queryWrapper.between("create_time", beginTime, endTime);
                List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
                logger.debug("查询成功");
                return ApiResult.success(clyjUserPropertyRecordList);
            }
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
            return ApiResult.fail(e.getMessage());
        }
        return null;
    }


    /**
     * 积分红包零钱明细
     *
     * @param userid
     * @return
     */
    @Override
    public ApiResult getAccountall(String userid, String asset) {
        // 获得本月第一天0点时间
        String m = DateUtil.getTimesMonthmorning();
        String date = DateUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss");

        try {
            //查询用户资产表
            QueryWrapper<ClyjUserPropertyRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userid);
            queryWrapper.eq("asset_type", asset);
            queryWrapper.between("create_time", m, date);
            List<ClyjUserPropertyRecord> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectList(queryWrapper);
            logger.debug("查询成功");
            return ApiResult.success(clyjUserPropertyRecordList);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
            return ApiResult.fail(e.getMessage());
        }
    }
}
